package com.ars.flight;

import com.ars.db.DBTools;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;

import javax.swing.*;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class AddScheduleController implements Initializable {
    @FXML
    private TextField tfAddressFrom;
    @FXML
    private TextField tfAddressTo;
    @FXML
    private TextField tfFlightName;
    @FXML
    private TextField tfHourStart;
    @FXML
    private TextField tfHourTo;
    @FXML
    private TextField tfMinStart;
    @FXML
    private TextField tfMinTo;
    @FXML
    private CheckBox cb1;
    @FXML
    private CheckBox cb2;
    @FXML
    private CheckBox cb3;
    @FXML
    private CheckBox cb4;
    @FXML
    private CheckBox cb5;
    @FXML
    private CheckBox cb6;
    @FXML
    private CheckBox cb7;
    @FXML
    private TextField tfLength;
    @FXML
    private TextField tfPrice;
    @FXML
    private ComboBox<String> cbPlaneName;
    @FXML
    private DatePicker dpDateStart;
    @FXML
    private DatePicker dpDateTo;

    //记录飞机名和飞机号
    private ArrayList<String> planes;
    private ArrayList<Integer> planeIds;
    //更新数据库所需
    private String flightName;
    private String addressFrom;
    private String addressTo;
    private String dateStart;
    private String dateEnd;
    private int planeID;
    private int hourStart;
    private int hourEnd;
    private int minStart;
    private int minEnd;
    private int length;
    private int price;
    private int schedule;            //表示班期的十进制表示法

    @FXML
    public void addButtonAction() {
        if(this.getFormValue()) {
            this.updateDB();
            JOptionPane.showMessageDialog(null,"操作成功！");
        }else{
            JOptionPane.showMessageDialog(null,"请将表单填写完整！");
        }
    }

    // Event Listener on Button.onAction
    @FXML
    public void cancelButtonAction() {
        tfAddressFrom.clear();
        tfAddressTo.clear();
        tfFlightName.clear();
        tfHourStart.clear();
        tfMinStart.clear();
        tfHourTo.clear();
        tfMinTo.clear();
        tfPrice.clear();
        tfLength.clear();
    }

    //获取表单数据
    private boolean getFormValue() {
        try {
            flightName = tfFlightName.getText();
            addressFrom = tfAddressFrom.getText();
            addressTo = tfAddressTo.getText();
            dateStart = dpDateStart.getValue().toString();
            dateEnd = dpDateTo.getValue().toString();
            hourStart = Integer.parseInt(tfHourStart.getText());
            hourEnd = Integer.parseInt(tfHourTo.getText());
            minStart = Integer.parseInt(tfMinStart.getText());
            minEnd = Integer.parseInt(tfMinTo.getText());
            length = Integer.parseInt(tfLength.getText());
            price = Integer.parseInt(tfPrice.getText());
            schedule = getSchedule();
            planeID = getSelectedPlan();
        }catch (Exception e){
            return false;
        }
        return !(flightName.isEmpty()||addressFrom.isEmpty()||addressTo.isEmpty()||dateStart.isEmpty()||dateEnd.isEmpty());
    }

    //获取飞机数组
    private void getPlanArray() {

        String sql = "select pid,name FROM plane";
        System.out.println(sql);
        ResultSet rs = new DBTools().query(sql);
        try {
            planes = new ArrayList<>();
            planeIds = new ArrayList<>();
            while (rs.next()) {
                planes.add(rs.getString("name"));
                planeIds.add(rs.getInt("pid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //获取选择的飞机
    private int getSelectedPlan() {
        for (int i = 0; i < planes.size(); i++) {
            if (planes.get(i).equals(cbPlaneName.getValue())) {
                return planeIds.get(i);
            }
        }
        return -1;
    }

    //获取班期
    private int getSchedule() {
        StringBuilder binary = new StringBuilder();
        binary.append(cb1.isSelected() ? 1 : 0);
        binary.append(cb2.isSelected() ? 1 : 0);
        binary.append(cb3.isSelected() ? 1 : 0);
        binary.append(cb4.isSelected() ? 1 : 0);
        binary.append(cb5.isSelected() ? 1 : 0);
        binary.append(cb6.isSelected() ? 1 : 0);
        binary.append(cb7.isSelected() ? 1 : 0);
        System.out.println(binary);
        System.out.println(Integer.parseInt(binary.toString(), 2));
        return Integer.parseInt(binary.toString(), 2);
    }

    //更新数据库
    private void updateDB() {
        String sql = "INSERT into `schedule` VALUES(null,'" + flightName + "','" + addressFrom + "','" + addressTo + "'," + length + "," + schedule + "," + price + ",'" + dateStart + "','" + dateEnd + "'," + hourStart + "," + minStart + "," + hourEnd + "," + minEnd + "," + planeID + ");";
        System.out.println(sql);
        new DBTools().update(sql);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        getPlanArray();
        cbPlaneName.getItems().addAll(planes);
    }
}
